const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    newGoods:[],
    hotGoods:[],
    topics:[],
    brands:[],
    floorGoods:[],
    banner:[],
    channel:[],
    floorstatus:false,
    rangeTop:0,
    userInfo: {},
  },
  swiperChange(e) {
    const that = this;
    that.setData({
      swiperIndex: e.detail.current
    })
  },
  onPageScroll:function(e){
    if(e.scrollTop > 100){
      this.setData({
        floorstatus:true
      });
    }else{
      this.setData({
        floorstatus:false
      });
    }
    this.setData({
      rangeTop: e.scrollTop
    });
  },
  goTop:function(e){
    if(wx.pageScrollTo){
      wx.pageScrollTo({
        scrollTop:0
      })
    }
  },
  onShareAppMessage: function () {
    let shareId = wx.getStorageSync('userId');
    wx.showModal({
      title: '提示',
      content: shareId,
      showCancel: false
    });
    return{
      title:'欢迎注册使用惠美猫！',
      path: '/pages/index/index?share_id=' + shareId,
      imageUrl: '/static/images/logo.png',
      success: function (res) { }
    }
  },
  onPullDownRefresh(){
	  	// 增加下拉刷新数据的功能
	    var self = this;
	    this.getIndexData();
 },
  getIndexData:function (){
    let that = this;
    var data = new Object();
    util.request(api.IndexUrlHotGoods).then(function (res) {
      if (res.errno === 0) {
        data.hotGoods = res.data.hotGoodsList
        that.setData(data);
      }
    });
    util.request(api.IndexUrlTopic).then(function (res) {
      if (res.errno === 0) {
        data.topics = res.data.topicList
      that.setData(data);
      }
    });
    util.request(api.IndexUrlBrand).then(function (res) {
      if (res.errno === 0) {
        data.brand = res.data.brandList
      that.setData(data);
      }
    });
    util.request(api.IndexUrlCategory).then(function (res) {
      if (res.errno === 0) {
        data.floorGoods = res.data.categoryList
        that.setData(data);
      }
    });
    util.request(api.IndexUrlBanner).then(function (res) {
      if (res.errno === 0) {
        data.banner = res.data.banner
      that.setData(data);
      }
    });
    util.request(api.IndexUrlChannel).then(function (res) {
      if (res.errno === 0) {
        data.channel = res.data.channel
      that.setData(data);
      }
    });
  },
  onShow: function () {
    let that = this;
    var data = new Object();
    util.requestIndex(api.IndexUrlNewGoods).then(function (res) {
      if (res.errno === 0) {
        data.newGoods = res.data.newGoodsList;
        that.setData(data);
      }
    });
  },
  onLoad:function(options){
    if (options != undefined) {
      const scene = decodeURIComponent(options.scene);
      if (scene != undefined){
        wx.setStorageSync('shareUserId', scene);
      }else{
        wx.setStorageSync('shareUserId', options.share_id);
      }
    }
    this.getIndexData();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})
